package com.sdhs.paas.rc.service;

import java.util.List;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sdhs.paas.sdk.base.common.BaseDao;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.github.pagehelper.Page;
import com.sdhs.paas.sdk.core.PaasBaseException;
import com.sdhs.paas.sdk.core.mybatis.EnablePaasMybatis;
import com.sdhs.paas.rc.bean.DocInfoBean;
import com.sdhs.paas.rc.repository.DocInfoMapper;
import com.sdhs.paas.sdk.base.common.entity.PaginationEntity;
import com.github.pagehelper.PageHelper;

/**
* 描述：DocInfoService 服务接口
* @author kangjichang
* @date 2018-12-03
*/

@EnablePaasMybatis
@MapperScan("com.sdhs.paas.rc.repository")
@Service
public class DocInfoService extends BaseDao<DocInfoBean, DocInfoMapper, Integer>{
		@Override
		public String getMapperNamespace() {
			return DocInfoMapper.class.getName();
		}
		
		@Autowired
		private DocInfoMapper docInfoMapper;
		
		public PaginationEntity<DocInfoBean> getPageDocInfoList(BasePage pageRequest, DocInfoBean docInfoBean) {
			Page<DocInfoBean> pageTotal = PageHelper.startPage(pageRequest.getPage(), pageRequest.getPageSize(),true, false);
			List<DocInfoBean> list = docInfoMapper.getList(docInfoBean);
			PaginationEntity<DocInfoBean> paginationEntity = new PaginationEntity<DocInfoBean>(list,
	                (int) pageTotal.getTotal());
			return paginationEntity;
		}
		
		/**
		 * 文件下载明细分页查询，doc_info表和doc_download_record关联查询
		 * */
		public PaginationEntity<DocInfoBean> getDocDownloadDetailPageList(BasePage pageRequest, DocInfoBean docInfoBean) {
			Page<DocInfoBean> pageTotal = PageHelper.startPage(pageRequest.getPage(), pageRequest.getPageSize(),true, false);
			List<DocInfoBean> list = docInfoMapper.getDocDownloadDetailList(docInfoBean);
			PaginationEntity<DocInfoBean> paginationEntity = new PaginationEntity<DocInfoBean>(list,
	                (int) pageTotal.getTotal());
			return paginationEntity;
		}
		
		
		@Transactional(rollbackFor = Exception.class)
		public boolean saveDocInfo(DocInfoBean docInfoBean) {
			try {
				//ucAppBean.setCtime(System.currentTimeMillis());
				//ucAppBean.setUtime(System.currentTimeMillis());
				int count = docInfoMapper.insertDocInfo(docInfoBean);
				return count == 1;
			} catch (Exception e) {
				throw new PaasBaseException(e, "500", "数据库插入异常！");
			}
		}
	
		@Transactional(rollbackFor = Exception.class)
		public boolean updateDocInfo(DocInfoBean docInfoBean) {
			try {
				//ucAppBean.setUtime(System.currentTimeMillis());
				int count = docInfoMapper.updateDocInfo(docInfoBean);
				return count == 1;
			} catch (Exception e) {
				throw new PaasBaseException(e, "500", "数据库插入异常！");
			}
		}
	
		public DocInfoBean getDocInfoDetail(String id) {
			return docInfoMapper.getDocInfoDetail(id);
		}
		
		public boolean deleteDocInfo(DocInfoBean docInfoBean){
			return docInfoMapper.deleteDocInfo(docInfoBean)>0;
		}
		
		public DocInfoBean getDocInfoByHashCode(String hashCode){
			return docInfoMapper.getDocInfoByHashCode(hashCode);
		}
      
}